<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<%@ include file="/WEB-INF/views/include/easyui.jsp"%>
</head>
<body class="easyui-layout" style="font-family: '微软雅黑'">
<div data-options="region:'center',split:true,border:false,title:'组织机构列表'">
<div id="tb" style="padding:5px;height:auto">
    <div>
    	<shiro:hasPermission name="sys:org:add">
    	<a href="#" class="easyui-linkbutton" plain="true" id="add" iconCls="icon-add" onclick="add();">添加</a>
    	<span class="toolbar-item dialog-tool-separator"></span>
    	</shiro:hasPermission>
        <shiro:hasPermission name="sys:org:del">
        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="del()">删除</a>
        <span class="toolbar-item dialog-tool-separator"></span>
      </shiro:hasPermission>
        <shiro:hasPermission name="sys:org:update">
        <a href="#" class="easyui-linkbutton" plain="true" id="upd" iconCls="icon-edit" onclick="upd()">修改</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="sys:org:grant">
        <span class="toolbar-item dialog-tool-separator"></span>
         <a href="#" class="easyui-linkbutton" plain="true"  id="orgForRole" iconCls="icon-hamburg-user" onclick="orgForRole()">授权</a>
    	</shiro:hasPermission>
    </div>
</div>
<table id="dg"></table>
</div>
<div data-options="region:'east',split:true,border:false,title:'用户列表'" style="width: 425px">
<div id="tg_tb" style="padding: 5px; height: auto">
			<div>
 <shiro:hasPermission name="sys:org:save">
				<a href="#" class="easyui-linkbutton" id="saveOrgUser" iconCls="icon-save"
					plain="true" onclick="saveOrgUser();">保存</a> <span
					class="toolbar-item dialog-tool-separator"></span>
</shiro:hasPermission>
			</div>
		</div>

		<table id="users"></table>
</div>
<div id="dlg"></div> 
<div id="icon_dlg"></div>  

<script type="text/javascript">
var dg;
var d;
var users;
var parentPermId;
var rolePerData;	//用户拥有的权限
$(function(){   
	dg=$('#dg').treegrid({  
	method: "get",
    url:'<%=path %>/system/organization/getUserOrgList', 
    cache: false, 
    fit : true,
	fitColumns : true,
	border : false,
	idField : 'id',
	treeField:'orgName',
	parentField : 'pid',
	animate:true, 
	rownumbers:true,
	singleSelect:true,
	striped:true,
    columns:[[    
        {field:'id',title:'id',hidden:true,width:100},    
        {field:'orgName',title:'机构名称',width:100},
        {field:'orgCode',title:'机构代码',width:100}
    ]],
    enableHeaderClickMenu: false,
    enableHeaderContextMenu: false,
    enableRowContextMenu: false,
    toolbar:'#tb',
    dataPlain: true,
    onClickRow:function(rowData){
    	if(rowData.orgType=='sysuser'){
    		users.datagrid('clearSelections');
    		rolePerData=[];//清空
    		//屏蔽按钮
    		lockButton();
			return;
    	}else{
    		users.datagrid('clearSelections');
    		parentPermId=rowData.id;
    		users.datagrid("load",{orgId:rowData.id});
    		//lookU(rowData.id);
    		unLockButton();
    	}
    	
    }
	});
	users=$('#users').datagrid({    
		method: "get",
	    url:'<%=path %>/system/user/orgList', 
	    fit : true,
		fitColumns : true,
		border : false,
		idField : 'id',
		striped:true,
		pagination:true,
		rownumbers:true,
		pageNumber:1,
		pageSize : 20,
		pageList : [ 10, 20, 30, 40, 50 ],
		toolbar:'#tg_tb',
	    columns:[[    
	        {field:'id',title:'id',hidden:true},    
	        {field:'loginName',title:'帐号',sortable:true,width:100},    
	        {field:'name',title:'姓名',sortable:true,width:100}
	    ]]
		});
});


function lockButton(){
	$("#add").hide();
	$("#upd").hide();
	$("#orgForRole").hide();
	$("#saveOrgUser").hide();
}

function unLockButton(){
	$("#add").show();
	$("#upd").show();
	$("#orgForRole").show();
	$("#saveOrgUser").show();
}

function saveOrgUser(){
	var row = dg.datagrid('getSelected');
	var orgId=row.id;
	parent.$.messager.confirm('提示', '确认要保存修改？', function(data){
	if (data){
		var newPermissionList=[];
		var data=users.datagrid('getSelections');
		for(var i=0,j=data.length;i<j;i++){
			newPermissionList.push(data[i].id);
		}
		if(orgId==null) {
			parent.$.messager.show({ title : "提示",msg: "请选择机构！", position: "bottomRight" });
			return;
		}
		$.ajax({
			async:false,
			type:'POST',
			data:JSON.stringify(newPermissionList),
			contentType:'application/json;charset=utf-8',
			url:"<%=path %>/system/organization/"+orgId+"/updateUserOrg",
			success: function(data){
				//重新加载树
				dg.treegrid('reload');
				successTip(data);
			}
		});
	} 
	});
}

//弹窗增加
function add() {
	//父级权限
	var row = dg.treegrid('getSelected');
	if(row){
		parentPermId=row.id;
	}
	
	d=$('#dlg').dialog({    
	    title: '添加菜单',    
	    width: 450,    
	    height: 320,    
	    closed: false,    
	    cache: false,
	    maximizable:true,
	    resizable:true,
	    href:'<%=path %>/system/organization/create',
	    modal: true,
	    buttons:[{
			text:'确认',
			handler:function(){
				$("#mainform").submit();
			}
		},{
			text:'取消',
			handler:function(){
					d.panel('close');
				}
		}]
	});
}

//删除
function del(){
	var row = dg.treegrid('getSelected');
	console.log(row.orgType);
	if(rowIsNull(row)) return;
	parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function(data){
		if (data){
			if(row.orgType=="organization"){//组织机构
				$.ajax({
					type:'get',
					url:"<%=path %>/system/organization/delete/"+row.id,
					success: function(data){
						if(successTip(data,dg))
				    		dg.treegrid('reload');
					}
				});
			}else{
				//用户
				
				var uid=(row.id).split("_");
				
				$.ajax({
					type:'get',
					url:"<%=path %>/system/organization/deleteUser/"+uid[1]+"?orgId="+uid[0],
					success: function(data){
						if(successTip(data,dg))
				    		dg.treegrid('reload');
					}
				});
			}
			
			//dg.datagrid('reload'); //grid移除一行,不需要再刷新
		} 
	});

}

//修改
function upd(){
	var row = dg.treegrid('getSelected');
	if(rowIsNull(row)) return;
	//父级权限
	parentPermId=row.pid;
	d=$("#dlg").dialog({   
	    title: '修改菜单',    
	    width: 450,    
	    height: 320,    
	    href:'<%=path %>/system/organization/update/'+row.id,
	    maximizable:true,
	    modal:true,
	    buttons:[{
			text:'确认',
			handler:function(){
				$("#mainform").submit();
			}
		},{
			text:'取消',
			handler:function(){
					d.panel('close');
				}
		}]
	});

}
//机构角色弹窗
function orgForRole(){
	var row = dg.datagrid('getSelected');
	if(rowIsNull(row)) return;
	$.ajaxSetup({type : 'GET'});
	d=$("#dlg").dialog({   
	    title: '机构角色管理',    
	    width: 580,    
	    height: 350,  
	    href:'<%=path %>/system/organization/'+row.id+'/orgRole',
	    maximizable:true,
	    modal:true,
	    buttons:[{
			text:'确认',
			handler:function(){
				saveOrgRole();
				d.panel('close');
			}
		},{
			text:'取消',
			handler:function(){
					d.panel('close');
			}
		}]
	});
}
var nowIcon;
var icon_dlg;
</script>
</body>
</html>